/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Createkey.java
 *
 * Created on Dec 23, 2011, 9:25:16 AM
 */
package rsaapp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import rsa.lib.Key;
import rsa.min.CreateKey;

/**
 *
 * @author User
 */
public class Createkey extends javax.swing.JFrame {

    /** Creates new form Createkey */
    public Createkey() {
        //this.jdad = parent;        
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator2 = new javax.swing.JSeparator();
        txtOutputFile = new javax.swing.JTextField();
        txtOutput = new javax.swing.JButton();
        txtKeySize = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtCreateKey = new javax.swing.JButton();
        lblInfo = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(rsaapp.RSAappApp.class).getContext().getResourceMap(Createkey.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        txtOutputFile.setFont(resourceMap.getFont("txtOutputFile.font")); // NOI18N
        txtOutputFile.setText(resourceMap.getString("txtOutputFile.text")); // NOI18N
        txtOutputFile.setName("txtOutputFile"); // NOI18N

        txtOutput.setText(resourceMap.getString("txtOutput.text")); // NOI18N
        txtOutput.setName("txtOutput"); // NOI18N
        txtOutput.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtOutputMouseClicked(evt);
            }
        });

        txtKeySize.setFont(resourceMap.getFont("txtKeySize.font")); // NOI18N
        txtKeySize.setText(resourceMap.getString("txtKeySize.text")); // NOI18N
        txtKeySize.setName("txtKeySize"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txtCreateKey.setText(resourceMap.getString("txtCreateKey.text")); // NOI18N
        txtCreateKey.setName("txtCreateKey"); // NOI18N
        txtCreateKey.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCreateKeyMouseClicked(evt);
            }
        });

        lblInfo.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblInfo.setText(resourceMap.getString("lblInfo.text")); // NOI18N
        lblInfo.setName("lblInfo"); // NOI18N

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem3.setText(resourceMap.getString("jMenuItem3.text")); // NOI18N
        jMenuItem3.setName("jMenuItem3"); // NOI18N
        jMenuItem3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuItem3MouseClicked(evt);
            }
        });
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jMenu1.add(jMenuItem2);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jMenu1.add(jSeparator1);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(rsaapp.RSAappApp.class).getContext().getActionMap(Createkey.class, this);
        jMenuItem1.setAction(actionMap.get("quit")); // NOI18N
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N

        jMenuItem4.setText(resourceMap.getString("jMenuItem4.text")); // NOI18N
        jMenuItem4.setName("jMenuItem4"); // NOI18N
        jMenu2.add(jMenuItem4);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtOutputFile, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtOutput)
                .addContainerGap(32, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtKeySize, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addContainerGap(12, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtCreateKey)
                .addContainerGap(293, Short.MAX_VALUE))
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(277, Short.MAX_VALUE)
                .addComponent(lblInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtOutputFile, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtKeySize, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(txtCreateKey, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jMenuItem3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem3MouseClicked
// TODO add your handling code here:
    jdad.setVisible(true);
    this.setVisible(false);
}//GEN-LAST:event_jMenuItem3MouseClicked

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
// TODO add your handling code here:
    jdad.setVisible(true);
    this.setVisible(false);
}//GEN-LAST:event_jMenuItem3ActionPerformed

private void txtOutputMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtOutputMouseClicked
// TODO add your handling code here:
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int returnVal = fc.showOpenDialog(null);

    if(returnVal == JFileChooser.APPROVE_OPTION){
        File file = fc.getSelectedFile();
        this.outfile = file;
        txtOutputFile.setText(file.getAbsolutePath());
    }else{
        this.outfile = null;
    }
}//GEN-LAST:event_txtOutputMouseClicked

private void txtCreateKeyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCreateKeyMouseClicked
// TODO add your handling code here:
    lblInfo.setText("Waiting...");
    int keylength = 1024;
    try{
        keylength = Integer.parseInt(txtKeySize.getText());
    }catch(NumberFormatException e){
        keylength = 1024;
    }
    
    if(keylength <= 0)
        keylength = 1024;
    
    String pkfile = "public.key";
    String skfile = "private.key";
    
    if(this.outfile != null){
        pkfile = outfile.getPath() + "\\" + pkfile;
        skfile = outfile.getPath() + "\\" +  skfile;
    }
    
    CreateKey k = new CreateKey(keylength/2);
    Key pk = k.getPublicKey();
    Key sk = k.getPrivateKey();
    try {
        pk.toFile(pkfile);
        sk.toFile(skfile);
    } catch (FileNotFoundException ex) {
        //Logger.getLogger(Createkey.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, "Unable to open file", "Error",JOptionPane.ERROR_MESSAGE);
    } catch (IOException ex) {
        //Logger.getLogger(Createkey.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, "Unable to save file", "Error",JOptionPane.ERROR_MESSAGE);
    }finally{
        lblInfo.setText("Finished");
    }
}//GEN-LAST:event_txtCreateKeyMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Createkey.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Createkey.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Createkey.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Createkey.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Createkey().setVisible(true);
            }
        });
    }
    
    public void setDad(JFrame jdad){
        this.jdad = jdad;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JButton txtCreateKey;
    private javax.swing.JTextField txtKeySize;
    private javax.swing.JButton txtOutput;
    private javax.swing.JTextField txtOutputFile;
    // End of variables declaration//GEN-END:variables
    private JFrame jdad;
    private final JFileChooser fc = new JFileChooser();
    File outfile;
}
